package com.rxh.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/2/6
 * Time: 15:19
 * Project: Management
 * Package: com.rxh.utils
 */
public class JsonUtils {
    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            return MAPPER.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将对象转换成json字符串空字段不添加。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     *
     * @param data
     * @return
     */
    public static String objectToJsonNonNull(Object data) {
        try {
            MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            return MAPPER.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            // 反序列化忽略Bean对象没有的字段
            MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return MAPPER.readValue(jsonData, beanType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 支持Json转带泛型类，
     * 如：Map<String, Object> -> jsonToPojo(String jsonData, Map.class, String.class, Object.class)
     *
     * @param jsonData Json字符串
     * @param beanType
     * @param elementType
     * @param <T>
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<?> beanType, Class<?>... elementType) {
        try {
            // 反序列化忽略Bean对象没有的字段
            MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return MAPPER.readValue(jsonData, MAPPER.getTypeFactory().constructParametricType(beanType, elementType));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T jsonToPojo(String jsonData, JavaType javaType) {
        try {
            // 反序列化忽略Bean对象没有的字段
            MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JavaType getJavaType(Type type) {
        return MAPPER.getTypeFactory().constructType(type);
    }

    public static JavaType getJavaType(Class<?> parametrized, Class<?>... parameterClasses) {
        return MAPPER.getTypeFactory().constructParametricType(parametrized, parameterClasses);
    }

    public static JavaType getJavaType(Class<?> rawType, JavaType... parameterTypes) {
        return MAPPER.getTypeFactory().constructParametricType(rawType, parameterTypes);
    }

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JsonNode json2JsonNode(String jsonStr) {
        try {
            return new ObjectMapper().readTree(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  将json数据转换成List<Map<String,Object></>
     */
     /*public static List<Map<String,Object>> jsonToListMap(String jsonData) {
         try {
             return MAPPER.readValue(jsonData,List.class);
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }*/

    /**
     *  将json数据转换成Map<String,Object></>
     */
    /*public static Map<String,Object> jsonToMap(String jsonData) {
        try {
            return MAPPER.readValue(jsonData, Map.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }*/
}
